package com.div.tools.popup

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.drawable.Drawable
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.PopupWindow

@SuppressLint("StaticFieldLeak")
object PopupTool {
    private val FLAG_FULL_SREEN: Int = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
    val TOP = 0x1
    val LEFT = 0x2
    val RIGHT = 0x3
    val BOTTOM = 0x4
    private var popupWindow: PopupWindow? = null
    private var width: Int = ViewGroup.LayoutParams.WRAP_CONTENT
    private var height: Int = ViewGroup.LayoutParams.WRAP_CONTENT
    private var isAtFullScreen = false
    private var isAlpha = false
    private var context: Activity? = null

    fun setContentView(context: Activity, view: View): PopupTool {
        this.context = context
        popupWindow = PopupWindow()
        popupWindow?.setOnDismissListener {
            if (isAlpha) {
                setBgAlpha(1.0f)
                this.isAlpha = false
            }
            this.context = null
            this.isAtFullScreen = false
            this.popupWindow = null
        }
        popupWindow?.contentView = view
        popupWindow?.width = width
        popupWindow?.height = height
        return this
    }

    private fun setBgAlpha(alpha: Float) {
        val attributes = context?.window?.attributes
        attributes?.alpha = alpha
        context?.window?.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        context?.window?.attributes = attributes
    }

    fun getContentView(): View {
        return popupWindow?.contentView as View
    }

    fun setBackgroundDrawable(background: Drawable): PopupTool {
        popupWindow?.setBackgroundDrawable(background)
        return this
    }

    fun setFocusable(focusable: Boolean): PopupTool {
        popupWindow?.isFocusable = focusable
        return this
    }

    fun setOutsideTouchable(outsideTouchable: Boolean): PopupTool {
        popupWindow?.isOutsideTouchable = outsideTouchable
        return this
    }

    fun isFocusable(): Boolean {
        return popupWindow?.isFocusable?: false
    }

    fun setAtFullScreen(isAtFullScreen: Boolean): PopupTool {
        this.isAtFullScreen = isAtFullScreen
        return this
    }

    fun showAtLocation(view: View, location: Int) {
        val array: IntArray = IntArray(2)
        view.getLocationInWindow(array)
        getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
        val measureWidth = getContentView().measuredWidth
        val measureHeight = getContentView().measuredHeight
        var x = array[0] + view.width - measureWidth/2
        var y = array[1] - measureHeight
        var gravity = Gravity.NO_GRAVITY
        when(location) {
            TOP -> {
                x = array[0] + view.width/2 - measureWidth/2
                y = array[1] - measureHeight
            }
            LEFT -> {
                x= array[0] - measureWidth
                y = array[1] + view.height/2 - measureHeight/2
            }
            RIGHT -> {
                x = array[0] + view.width
                y = array[1] + view.height/2 - measureHeight/2
            }
            BOTTOM -> {
                x = array[0] + view.width/2 - measureWidth/2
                y = array[1] + view.height
            }
        }
        if (isAlpha) {
            setBgAlpha(0.5f)
        }
        // 解决全屏下 Focusable = true， 显示 PopupWindow 后底部导航栏弹出问题
        val focusable = isFocusable()
        // 保证 PopupWindow 展示的时候导航栏不弹出。
        setFocusable(false)
        popupWindow?.showAtLocation(view, gravity, x, y)
        if (isAtFullScreen && focusable) {
            addFullScreenFlag()
        }
        // 还原 Focusable 设置
        setFocusable(focusable)
        // 刷新 PopupWindow
        update()
    }

    fun update() {
        popupWindow?.update()
    }

    private fun addFullScreenFlag() {
        getContentView().systemUiVisibility = FLAG_FULL_SREEN
    }

    fun showAsDropDown(view: View) {
        popupWindow?.showAsDropDown(view)
    }

    fun showAlpha(isAlpha: Boolean): PopupTool {
        this.isAlpha = isAlpha
        return this
    }
}